<?php
declare (strict_types=1);

namespace app\api\exception;

use app\api\common\Auth;
use app\api\common\common;
use app\api\common\Output;

use app\api\controller\v1\System;
use app\api\middleware\CheckApi;
use app\api\middleware\CheckLogin;

use app\api\middleware\Cores;
use app\BaseController;
use Closure;
use think\App;
use think\facade\Db;
use think\facade\Cache;
use think\facade\Log;
use think\middleware\AllowCrossDomain;
use think\Request;

/**
 * api基础类
 */
 abstract class BaseException
{
    use Output;

    /**
     * Request实例
     * @var \think\Request
     */
    protected $request;
    protected $user_id;
    protected $user_type;
    /**
     * 应用实例
     * @var \think\App
     */
    protected $app;

    /**
     * 控制器中间件
     * @var array
     */
    protected $middleware = [];

    /**
     * 不需要登录的方法
     */
    protected $noAuth = [];

    protected $page_num = 1;

    protected $page_limit = 10;
    /**
     * BaseException constructor.
     * @param Request $request
     */


    public function __construct(Request $request)
    {
        $this->request = $request;
        $requestInfo = [
            'ip' => $request->ip(),
            'method' => $request->method(),
            'host' => $request->host(),
            'url' => $request->url(),
            'header' => $request->header()
        ];
        $logInfo = [
            "{$requestInfo['ip']} {$requestInfo['method']} {$requestInfo['host']}{$requestInfo['url']}",
            '[ ROUTE ] ' . var_export($this->getRouteInfo(), true),
            '[ HEADER ] ' . var_export($request->header(), true),
            '[ PARAM ] ' . var_export($request->param(), true),
            '---------------------------------------------------------------',
        ];

        $logInfo = implode(PHP_EOL, $logInfo) . PHP_EOL;
        Log::record($logInfo, 'info');
        Log::record("123", 'info');
        $this->init();


    }



    /**
     * 获取路由信息
     * @return array
     */
    protected function getRouteInfo(): array
    {
        $request = app(Request::class);
        return [
            'rule' => $request->rule()->getRule(),
            'route' => $request->rule()->getRoute(),
            'option' => $request->rule()->getOption(),
            'var' => $request->rule()->getVars(),
        ];
    }

    public function init()
    {
        Cores::class;
        $header = $this->request->header();
        // 检测方法是否需要验证登录
        if (in_array($this->request->action(), $this->noAuth)) {
            return true;
        } else {//控制器中间件

            $this->middleware = [
                CheckLogin::class,
                CheckApi::class
            ];
            if (!empty($header["sign"])){
                $userpermission = Db::name("permission")->where(["sign" => $header["sign"],"data_state" => 1])->find();
                if ($userpermission) {
                    $this->user_id = $userpermission["user_id"];
                    $this->user_type = $userpermission["user_type"];
                }
            }else{
                $common = new common();
                return $common->result(null,'请先登陆！', 100001);
            }
        }
    }

}

